package maa.action;

import maa.action.MappedAction;
import maa.dao.UserDAO;
import maa.entity.Gender;
import maa.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;

@MappedAction(get = "/register", post = "/register")
public class RegisterPost implements Action {
    @Override
    public ActionResult execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        ActionResult actionResult = new ActionResult();
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String repeatedPassword = req.getParameter("password_check");
        String surname = req.getParameter("surname");
        Date birthdate = Date.valueOf(req.getParameter("birthdate"));
        Gender gender = Gender.valueOf(req.getParameter("gender").toUpperCase());
        UserDAO dao = UserDAO.getInstance();
        if (password.equals(repeatedPassword)) {
            User user = dao.createUser(email, password, name, surname, birthdate, gender);
            dao.addUser(user);
            actionResult.setResult("account");
            actionResult.setRedirected(true);
        } else {
            actionResult.setResult("index");
            actionResult.setRedirected(false);
        }
        return actionResult;
    }
}
